﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CardServices.DataAccess
{
    public class CardManager
    {
        public CardManager()
        {
            
        }

        public bool ShuffleDeck()
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var cards = from card in context.HeldBies
                            where card.PlayerID != 0
                            select card;

                foreach (HeldBy card in cards)
                {
                    card.PlayerID = 0;
                }

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Adds a new player to the list of available players.
        /// </summary>
        /// <param name="PlayerName"></param>
        /// <returns></returns>
        public int AddPlayer(String PlayerName)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                Player newPlayer = new Player();
                newPlayer.PlayerName = PlayerName;
                context.Players.InsertOnSubmit(newPlayer);
                context.SubmitChanges();
            }
            return 1;
        }

        /// <summary>
        /// Deletes a player by Name
        /// </summary>
        /// <param name="PlayerName"></param>
        /// <returns></returns>
        public bool DeletePlayer(String PlayerName)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var players =  from player in context.Players
                          where player.PlayerName.ToLower().Equals(PlayerName.ToLower())
                          select player;

                context.Players.DeleteAllOnSubmit(players);

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Deletes a player by ID
        /// </summary>
        /// <param name="PlayerID"></param>
        /// <returns></returns>
        public bool DeletePlayer(int PlayerID)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var players = from player in context.Players
                              where player.PlayerID.Equals(PlayerID)
                              select player;

                context.Players.DeleteAllOnSubmit(players);

                context.SubmitChanges();
            }
            return true;
        }

        /// <summary>
        /// Transfer ownership of a card.
        /// Ex. 1) The dealer hands the card to player 1.
        /// Ex. 2) Player 1 plays a card on the table. 
        /// </summary>
        /// <param name="CardID"></param>
        /// <param name="PlayerID"></param>
        /// <returns></returns>
        public bool TransferCard(int CardID, int PlayerID)
        {
            using (CardLINQDataContext context = new CardLINQDataContext())
            {
                var cards = from heldby in context.HeldBies
                            where (heldby.CardID.Equals(CardID))
                            select heldby;
                foreach (HeldBy card in cards)
	            {
            		 card.PlayerID = PlayerID;
	            }

                context.SubmitChanges();
            }

            return true;
        }
    }
}
